package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcProjectSgazht;
import com.ruoyi.gcgl.domain.GcProjectSgazhtFj;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcProjectSgazhtFjService;
import com.ruoyi.gcgl.service.IGcProjectSgazhtService;

/**
 * 施工安装合同附件Controller
 * 
 * @author ruoyi
 * @date 2023-01-23
 */
@Controller
@RequestMapping("/gcgl/sgazhtFj")
public class GcProjectSgazhtFjController extends BaseController
{
    private String prefix = "gcgl/sgazhtFj";

    @Autowired
    private IGcProjectSgazhtFjService gcProjectSgazhtFjService;
    @Autowired
    private IGcProjectSgazhtService gcProjectSgazhtService;
    @Autowired
    private IGcProjectService gcProjectService;

    @RequiresPermissions("gcgl:sgazhtFj:view")
    @GetMapping()
    public String sgazhtFj()
    {
        return prefix + "/sgazhtFj";
    }

    /**
     * 查询施工安装合同附件列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcProjectSgazhtFj gcProjectSgazhtFj)
    {
        startPage();
        gcProjectSgazhtFj.setDeleteFlag(1);
        gcProjectSgazhtFj.setType("2");
        if(getSysUser().isShow()){
        gcProjectSgazhtFj.setCreateUser(getLoginName());
        }
        List<GcProjectSgazhtFj> list = gcProjectSgazhtFjService.selectGcProjectSgazhtFjList(gcProjectSgazhtFj);
        
        for(GcProjectSgazhtFj zjbj:list){
        	if(zjbj.getZilwj()!=null && !zjbj.getZilwj().equals("")){
        	zjbj.setZilwj(RuoYiConfig.getOnPath()+zjbj.getZilwj());
        	}
        }
        return getDataTable(list);
    }

    /**
     * 导出施工安装合同附件列表
     */
    @Log(title = "施工安装合同附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcProjectSgazhtFj gcProjectSgazhtFj)
    {
        List<GcProjectSgazhtFj> list = gcProjectSgazhtFjService.selectGcProjectSgazhtFjList(gcProjectSgazhtFj);
        ExcelUtil<GcProjectSgazhtFj> util = new ExcelUtil<GcProjectSgazhtFj>(GcProjectSgazhtFj.class);
        return util.exportExcel(list, "施工安装合同附件数据");
    }

    /**
     * 新增施工安装合同附件
     */
    @GetMapping("/add/{sgazhtId}")
    public String add(ModelMap mmap,@PathVariable("sgazhtId") Long sgazhtId)
    {
    	GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(sgazhtId);
    	mmap.put("gcProjectSgazht", gcProjectSgazht);
        return prefix + "/add";
    }

    /**
     * 新增保存施工安装合同附件
     */
    @Log(title = "施工安装合同附件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcProjectSgazhtFj gcProjectSgazhtFj,@RequestParam("zilwjfile")MultipartFile filewj)
    {
    	try {
    		if (!filewj.isEmpty()){
                String zilwj = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), filewj, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
                gcProjectSgazhtFj.setZilwj(zilwj);
            }
    		gcProjectSgazhtFj.setType("2");
    		gcProjectSgazhtFj.setDeleteFlag(1);
    		gcProjectSgazhtFj.setCreateAt(new Date());
    		gcProjectSgazhtFj.setUpdateAt(new Date());
    		gcProjectSgazhtFj.setCreateUser(getLoginName());
    		gcProjectSgazhtFj.setUpdateUser(getLoginName());
    		gcProjectSgazhtFjService.insertGcProjectSgazhtFj(gcProjectSgazhtFj);
		} catch (Exception e) {
			return error(e.getMessage());
		}
        return toAjax(1);
    }

    /**
     * 修改施工安装合同附件
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSgazhtFj gcProjectSgazhtFj = gcProjectSgazhtFjService.selectGcProjectSgazhtFjById(id);
        if(gcProjectSgazhtFj.getZilwj()!=null && !gcProjectSgazhtFj.getZilwj().equals("")){
        	gcProjectSgazhtFj.setZilwj(RuoYiConfig.getOnPath()+gcProjectSgazhtFj.getZilwj());
        }
        mmap.put("gcProjectSgazhtFj", gcProjectSgazhtFj);
        
        GcProjectSgazht gcProjectSgazht = 
        		gcProjectSgazhtService.selectGcProjectSgazhtById(gcProjectSgazhtFj.getSgazhtId());
        mmap.put("gcProjectSgazht", gcProjectSgazht);
        return prefix + "/edit";
    }

    /**
     * 修改保存施工安装合同附件
     */
    @Log(title = "施工安装合同附件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcProjectSgazhtFj gcProjectSgazhtFj,@RequestParam("zilwjfile")MultipartFile filewj)
    {
    	try{
            if (!filewj.isEmpty()){
                String zilwj = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), filewj, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
                gcProjectSgazhtFj.setZilwj(zilwj);
            }
            gcProjectSgazhtFj.setUpdateAt(new Date());
            gcProjectSgazhtFj.setUpdateUser(getLoginName());
    		gcProjectSgazhtFjService.updateGcProjectSgazhtFj(gcProjectSgazhtFj);
        }
        catch (Exception e){
            return error(e.getMessage());
        }
        return toAjax(1);
    }

    /**
     * 删除施工安装合同附件
     */
    @RequiresPermissions("gcgl:sgazhtFj:delete")
    @Log(title = "施工安装合同附件", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcProjectSgazhtFjService.deleteGcProjectSgazhtFjByIds(ids));
    }
}